package com.project.service.impl.backend.basic;

import java.util.List;

import com.project.dao.IMenuDao;
import com.project.dao.IMenuRelationDao;
import com.project.dao.bean.Menu;
import com.project.dao.bean.MenuRelation;
import com.project.service.backend.basic.IMenuService;
import com.project.service.impl.BaseServiceImpl;
import com.project.webapp.form.backend.basic.MenuForm;

/**
 * 后台 菜单信息管理 Service Impl
 * @author Sn
 *
 */
public class MenuServiceImpl extends BaseServiceImpl implements IMenuService 
{

    private IMenuDao menuDao;
    private IMenuRelationDao menuRelationDao;

    @Override
    public void save(MenuForm menuForm) throws Exception 
    {
        if (menuForm.getMenuId() == null || "".equals(menuForm.getMenuId())) 
        {
            /**
             * 新增
             */
            String menuId = menuDao.save(menuForm);

            MenuRelation mr = null;
            //将新增的这条菜单信息写入关系表
            mr = new MenuRelation();
            mr.setAncestorId(menuId);
            mr.setDescendantId(menuId);
            mr.setParentId(menuForm.getAncestorId());
            menuRelationDao.save(mr);

            if ("1".equals(menuForm.getAncestorId())) 
            {
                //基类菜单
            } else 
            {
                //将对应的上级菜单信息写入关系表
                mr = new MenuRelation();
                mr.setAncestorId(menuForm.getAncestorId());
                mr.setDescendantId(menuId);
                menuRelationDao.save(mr);

                //查找出所选父菜单的上级菜单
                List<String> ancestorIdList = menuRelationDao.getAncestorIdByDescendantId(menuForm
                    .getAncestorId());
                if (ancestorIdList != null && ancestorIdList.size() > 0) 
                {
                    for (String ancestorId : ancestorIdList) 
                    {
                        mr = new MenuRelation();
                        mr.setAncestorId(ancestorId);
                        mr.setDescendantId(menuId);
                        menuRelationDao.save(mr);
                    }
                }
            }

        } else 
        {
            /**
             * 修改
             */
            menuDao.update(menuForm);
            //menuRelationDao.save(menuForm);
        }
    }

    public void setMenuDao(IMenuDao menuDao) {
        this.menuDao = menuDao;
    }

    @Override
    public List<Menu> findMenuByPg(MenuForm menuForm) throws Exception {
        return menuDao.findMenuByPg(menuForm);
    }

    @Override
    public Integer findCount(MenuForm menuForm) throws Exception {
        return menuDao.findCount(menuForm);
    }

    @Override
    public void delete(MenuForm menuForm) throws Exception {
        menuDao.delete(menuForm);
        menuRelationDao.delete(menuForm);
    }

    public void setMenuRelationDao(IMenuRelationDao menuRelationDao) {
        this.menuRelationDao = menuRelationDao;
    }

    @Override
    public List<Menu> findMenuForTree() throws Exception {
        return menuDao.findMenuForTree();
    }

    @Override
    public boolean isHaveSubMenu(MenuForm menuForm) throws Exception {
        return menuRelationDao.isHaveSubMenu(menuForm);
    }

}
